<?php
namespace Duobao\Controller;

use Duobao\Model\MemberModel as Member;
use Duobao\Model\MemberRecordModel as Record;
use Duobao\Controller\LogController as Mylog;

class MemberController extends ControllerBase{

    /**
     * 默认
     */
    public function index()
    {
        echo "<!--hi , i'm from " . MODULE_NAME . "~" . APP_STATUS, "-->";
    }

    /**
     * 获取用户名
     */
    public function getUserName()
    {
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try {
            $id = $this->getUid(I('get.token'));

            if(is_numeric($id)){
                $model = new Member();
                $restult = $model->getUserName($id);

                if($restult){
                    $ret['data']['username']=$restult;
                }else{
                    $ret['data']['username']='';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 获取用户的余额
     */
    public function getUserMoney(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = $this->getUid(I('get.token'));

            if(is_numeric($id)){
                $model = new Member();
                $money = $model->getUserMoney($id);

                if($money){
                    $ret['data']['money']=$money;
                }else{
                    $ret['data']['money']='';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }


        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }


    /**
     * 获取用户的头像
     */
    public function getUserPhoto(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = $this->getUid(I('get.token'));

            if(is_numeric($id)){
                $model = new Member();
                $img = $model->getUserPhoto($id);

                if(!empty($img)){
                    $ret['data']['img']=$img;
                }else{
                    $ret['data']['img']='';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }
        }catch(\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 获取用户的手机号码
     */
    public function getUserPhone(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = $this->getUid(I('get.token'));

            if(is_numeric($id)){
                $model = new Member();

                $phone = $model->getUserPhone($id);

                if($phone){
                    $ret['data']['phone'] = $phone;
                }else{
                    $ret['data']['phone'] = '';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }

        }catch(\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 获取用户中心所需的数据
     */
    public function getUserCenter(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = $this->getUid(I('get.token'));
            #Mylog::write("member中的id".$id);
            if(is_numeric($id)){
                $model = new Member();

                $user = $model->userCenter($id);

                if($user) {
                    $ret['data']['item']=$user;
                }else{
                    $ret['data']['item'] = '';
                }
//                ob_start();
//                var_dump($user);
//                Mylog::write(ob_get_contents());
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }

        }catch(\Exception $e){
            $ret['msg'] = $e->getMessage();
            $ret['status'] = $e->getCode();
        }

        return $this->response($ret);
    }

    /**
     * 获取用户信息（用户中心点击用户进入的位置）
     */
    public function getUserMessage(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = $this->getUid(I('get.token'));

            if(is_numeric($id)){
                $model = new Member();
                $userMessage = $model->getUserMessage($id);

                if($userMessage){
                    $ret['data']['item'] = $userMessage;
                }else{
                    $ret['data']['item'] = '';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }

        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 获取用户的地址列表
     * 返回的地址是地址数组
     */
    public function getUserAddressList(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = $this->getUid(I('get.token'));

            if(is_numeric($id)){
                $model = new Member();

                $addressList = $model->getUserAddress($id);

                if($addressList){
                    $ret['data']['item'] = $addressList;
                }else{
                    $ret['data']['item']= '';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }
        }catch(\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 获取用户的默认地址
     */
    public function getUserDefaultAddress(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = $this->getUid(I('get.token'));

            if(is_numeric($id)){
                $model = new Member();

                $uid = $this->getUid(I('get.token'));

                $defaultAddr = $model->getUserDefaultAddress($uid);

                if($defaultAddr){
                    $ret['data']['item'] = $defaultAddr;
                }else{
                    $ret['data']['item'] = '';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /** Set Function Start **/
    /**
     * 设置用户的用户名
     * 用户id $uid
     * 用户名 $uname
     */
    public function setUserName(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = $this->getUid(I('get.token'));

            if(is_numeric($id)){
              $model = new Member();

                $uname = I('post.username');
                #禁止用户输入特殊字符
                foreach(C('FILTER_CHAR') as $v){
                    if(strpos($uname,$v)){
                        E('用户名包含非法字符',403);
                    }
                }

                if(empty($id) || empty($uname)){
                    $ret['status'] = 400;
                    $ret['msg'] = "用户id或者用户名为空";
                }else{
                    $restult = $model->setUserName($id,$uname);

                    if($restult == 'ok'){
                        $ret['data']['result'] = $restult;
                    }else{
                        $ret['data']['result'] = $restult;
                        $ret['status'] = 400;
                        $ret['msg'] = '更新失败，可能原因是用户名未改变或者更新发生错误';
                    }
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 修改手机号码,废弃2015-12-29
     */
    private function setUserPhone(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = $this->getUid(I('get.token'));
            if(is_numeric($id)){
                $model = new Member();

                $phone = I('post.phone');

                if(!empty($phone)){
                    $result = $model->setUserPhone($id,$phone);

                    if($result){
                        $ret['data']['result'] = $result;
                    }
                }else{
                    $ret['status'] = 403;
                    $ret['msg'] = "empty of phone";
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }


    /**
     * 从商品页面，点击用户名的时候进入的用户中心,其他用户的夺宝记录
     * 获取用户夺宝记录
     * 其他的用户查看仅限50条
     */
    public function getOtherBuyRecord(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $uid = I('post.id');
            $offset = I('post.offset');
            $length = I('post.limit');

            if(empty($offset)){
                $offset = 0;
            }
            if(empty($length)){
                $length =10;
            }

            if(empty($uid)){
                $ret['status'] = '403';
                $ret['msg'] = 'Gets the parameters ID is empty';//获取的参数id为空
            }else{
                $model = new Record();

                $result = $model->getOtherBuyRecord($uid,$offset,$length);
                if($result){
                    $ret['data']['item'] = $result;
                }else{
                    $ret['msg'] = "This user id buy record is empty";#
                    $ret['status'] = 200;
                }
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

       return $this->response($ret);
    }

    /**
     * 获取用户的中奖记录
     * 其他的人查看仅限50条
     */
    public function getUserWinRecord(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{

            $uid = I('post.id');#获取用户id
            $offset = I('post.offset');
            $length = I('post.limit');

            if(empty($offset)){
                $offset = 0;
            }
            if(empty($length)){
                $length = 10;
            }

            if(!empty($uid) && is_numeric($uid)){
                $model = new Record();

                $result = $model->getUserWinRecord($uid,$offset,$length);

                if($result){
                    $ret['data']['item'] = $result;
                }else{
                    #返回数据为空的msg
                    $ret['msg'] = 'This user win record is empty';#用户中奖记录为空
                    $ret['status'] = 200;
                }
            }else{
                $ret['msg'] = "Gets the parameters ID is empty Or ID is not a number.";#
                $ret['status'] = 403;
            }
        }catch (\Exception $e){
            $ret['status'] = $e-> getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 统计用户的夺宝记录条数
     */
    public function getUserBuyCount(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try {
            $uid = I('post.id');

            if(empty($uid)){
                $ret['status'] = 403;
                $ret['msg'] = 'user ID is empty';
            }else{
                $model = new Record();
                $result = $model->getUserBuyCount($uid);
                if($result){
                    $ret['data']['item'] = $result;#返回统计结果
                }else{
                    $ret['status'] = 200;
                    $ret['msg'] = 'empty of user buy record';#返回空的数据;
                }
            }
        }catch (\Exception $e){
            $ret['status'] = $e-> getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 统计用户的中奖记录
     */
    public  function getUserWinCount(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try {
            $uid = I('post.id');

            if(empty($uid)){
                $ret['status'] = 403;
                $ret['msg'] = 'user ID is empty';
            }else{
                $model = new Record();

                $result = $model->getUserWinCount($uid);
                if($result){
                    $ret['data']['item'] = $result;#返回统计结果
                }else{
                    $ret['status'] = 200;
                    $ret['msg'] = 'empty of the user win record';#返回中奖结果为0
                }
            }

        }catch (\Exception $e){
            $ret['status'] = $e-> getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户的充值记录，也就是用户的夺宝支付记录
     */
    public function getUserPayRecord(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = $this->getUid(I('get.token'));

            if(is_numeric($id)){
                    $offset = I('post.offset');
                    $length = I('post.limit');

                    if(empty($length)){
                        $length = 10;
                    }

                    if(empty($length)){
                        $length = 0;
                    }

                    $model = new Record();

                    $result = $model->getUserPayRecord($id,$offset,$length);

                    if($result){
                        $ret['data']['item'] = $result;
                    }else{
                        $ret['status'] = 200;
                        $ret['msg'] = 'the user have nothing pay order';
                    }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;
            }
        }catch(\Exception $e){
            $ret['status'] = $e->getMessage();
            $ret['msg'] = $e->getCode();
        }

        return $this->response($ret);
    }

    /**
     * 添加用户的收货地址
     */
    public function addUserAddress(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $uid  = $this->getUid(I('get.token'));
            $content = I('post.content');
            $default = I('post.default');

            //如果没有传递是否是默认地址，默认为false
            if(empty($default)){
                $default = false;
            }else{
                $default = true;
            }
            if(!empty($content)){
                if(is_numeric($uid)){
                    $model = new Member();
                    $result = $model->addUserAddress($uid, $content,$default);
                    if($result){
                        $ret['data']['status'] = true;
                    }else{
                        $ret['data']['status'] = false;
                        $ret['data']['msg'] = '新增地址失败可能是传入的参数不完整';
                    }
                }else{
                    $ret['code'] = 403;
                    $ret['msg'] = $uid;
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = 'empty of content';
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户删除的收货地址
     */
    public function delUserAddress(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $addressId = I('post.addressId');
            $uid = $this->getUid(I("get.token"));

            if(!empty($addressId)){
                if(is_numeric($uid)){
                    $model = new Member();
                    $result = $model->delUserAddress($uid,$addressId);

                    if($result){
                        $ret['data']['status'] = true;
                    }else{
                        $ret['data']['status'] = false;
                        $ret['data']['msg'] = '删除失败，当前用户不存在该地址或者程序异常';
                    }
                }else{
                    $ret['status'] = 403;
                    $ret['msg'] =$uid;
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = 'empty of address id';
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户设置地址为默认地址
     */
    private function userAddDefaultAddr(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $addressId = I('post.addressId');#用户地址id
            $uid = $this->getUid(I('get.token'));#用户uid

            if(!empty($addressId)){
                if(is_numeric($uid)){
                    $model = new Member();
                    $result = $model->userAddDefaultAddr($uid,$addressId);

                    if($result){
                        $ret['data']['status'] = $result;
                    }else{
                        $ret['data']['status'] = $result;
                        $ret['data']['msg'] = "修改失败,可能原因用户与对应地址不匹配或数据库修改异常";
                    }
                }else{
                    $ret['status'] = 403;
                    $ret['msg'] = $uid;
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = "empty of address id";
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户修改用户的收获地址
     */
    public function updateUserAddress(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $addressid = I('post.addressId');
            $content = I('post.content');#'省:市:县:街道:邮编:收货人:电话';
            $id = $this->getUid(I('get.token'));
            $default = I('post.default');

            if(is_numeric($id)){
                if(!empty($addressid)){
                    $model = new Member();
                    if(empty($default) || $default == false){
                        $result = $model->updateUserAddress($id,$addressid,$content);
                    }else{
                        $result = $model->updateUserAddress($id,$addressid,$content);
                        $result2 = $model->userAddDefaultAddr($id,$addressid);
                    }

                    if($result && $result2){
                        $ret['data']['status'] = $result;
                    }else{
                        $ret['data']['status'] = $result;
                        $ret['data']['msg'] = '修改地址失败或者地址id与用户id不匹配';
                    }
                }else{
                    $ret['status'] = 403;
                    $ret['msg'] = 'empty of address id';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = $id;#token 失效
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 点击点击商品，进入其他用户的个人中心显示的所有数据
     * 参数用户的id
     */
    public function getOtherMessage(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $uid = I('get.id');#获取用户的id
            if(!empty($uid) || is_numeric($uid)){
                $model = new Member();
                $result = $model->getOtherMessage($uid);
                if($result){
                    $ret['data'] = $result;
                }else{
                    $ret['data'] = 'user not exist';
                }
            }else{
                $ret['status'] = 403;
                $ret['message'] = 'empty of user id or user id is not a number';
            }

        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户点击自己的个人中心，进入的夺宝记录,全部的记录包含进行中和已揭晓的
     */
    public function getMyIndiana(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{

            #获取token
            $uid = $this->getUid(I('get.token'));
            $offset = I('post.offset');
            $length = I('post.limit');

            #设置offset与limit的默认值
            if(empty($offset)){
                $offset = 0;
            }
            if(empty($length)){
                $length = 10;
            }

            #验证uid是否有效
            if(is_numeric($uid)){
                $model = new Record();
                $result = $model->getMyIndiana($uid,$offset,$length);
                $ret['data'] = $result;
            }else{
                $ret['status'] = '403';
                $ret['msg'] = $uid;//token invalid
            }

        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户点击自己的个人中心，进入的夺宝记录,全部的记录包含进行中
     */
    public function getMyUnderway(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{

            #获取token
            $uid = $this->getUid(I('get.token'));
            $offset = I('post.offset');
            $length = I('post.limit');

            #设置offset与limit的默认值
            if(empty($offset)){
                $offset = 0;
            }
            if(empty($length)){
                $length = 10;
            }

            #验证uid是否有效
            if(is_numeric($uid)){
                $model = new Record();
                $result = $model->getMyUnderway($uid,$offset,$length);
                $ret['data']['underway'] = $result;
            }else{
                $ret['status'] = '403';
                $ret['msg'] = $uid;//token invalid
            }

        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }


    /**
     *  用户点击自己的个人中心，进入的夺宝记录,全部的记录已揭晓的数据
     */
    public function getMyAlreadyAnnounced(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{

            #获取token
            $uid = $this->getUid(I('get.token'));
            $offset = I('post.offset');
            $length = I('post.limit');

            #设置offset与limit的默认值
            if(empty($offset)){
                $offset = 0;
            }
            if(empty($length)){
                $length = 10;
            }

            #验证uid是否有效
            if(is_numeric($uid)){
                $model = new Record();
                $result = $model->getMyAlreadyAnnounced($uid,$offset,$length);
                $ret['data']['complete'] = $result;
            }else{
                $ret['status'] = '403';
                $ret['msg'] = $uid;//token invalid
            }

        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 获取用户自己的中奖记录的逻辑
     */
    public function getMyWinRecord(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{

            $uid = $this->getUid(I('get.token'));#获取用户id
            $offset = I('post.offset');
            $length = I('post.limit');

            if(empty($offset)){
                $offset = 0;
            }
            if(empty($length)){
                $length = 10;
            }

            if(is_numeric($uid)){
                $model = new Record();

                $result = $model->getMyWinRecord($uid,$offset,$length);

                if($result){
                    $ret['data']['item'] = $result;
                }else{
                    #返回数据为空的msg
                    $ret['msg'] = 'This user win record is empty';#用户中奖记录为空
                    $ret['status'] = 200;
                }
            }else{
                $ret['msg'] = $uid;#
                $ret['status'] = 403;
            }
        }catch (\Exception $e){
            $ret['status'] = $e-> getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 废弃
     * 向用户展示当前用户的某一个商品的中奖后的状态信息
     * 获取中奖商品的信息和状态
     * 接收参数，token，商品id
     */
    public function getWinGoodsMessageAndStatus(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            #获取用户的uid
            $uid = $this->getUid(I("get.token"));
            $gid = I('post.gid');#商品id

            if(!empty($gid)){
                if(is_numeric($uid)){
                    #调用接口
                }else{
                    $ret['status'] = 403;
                    $ret['msg'] = $uid;
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = 'good id is empty';
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 中奖后的逻辑：用户点击个人中心里面的中奖记录的商品，进入商品发货的逻辑，用户选择收货地址，并显示商品发货状态
     * fn:显示个人的中奖商品的状态信息
     */
    public function showMyWinGoodsStatusMsg(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            #获取用户uid和商品id，判断用户是否和商品中奖对应
            $uid = $this->getUid(I('get.token'));
            $gid = I('post.gid');

            if(empty($gid)){
                E("empty of goos id",403);
            }
            if(!is_numeric($uid)){
                E($uid,403);
            }

            #调用model层里面的中奖商品的状态的信息
            $model = new Member();
            $result = $model->showMyWinGoodsStatusMsg($uid,$gid);

            if($result){
                $ret['data'] = $result;
            }else{
                E("发生错误",403);
            }

        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户中奖商品,添加中奖商品的发送地址
     */
    public function addGoodsSendAddr(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $addressId = I('post.addressId');
            $uid = $this->getUid(I('get.token'));
            $gid = I('post.gid');

            if(empty($gid)){
                E("empty of goods id",403);
            }

            if(empty($addressId)){
                E('empty of address id',403);
            }

            if(!is_numeric($uid)){
                E($uid,403);
            }

            $model = new Member();
            $result = $model->addGoodsSendAddr($uid,$addressId,$gid);

            if($result){
                $ret['data'] = $result;
            }else{
                E("false",403);
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户中奖商品，修改中奖商品的发送地址
     */
    public function updateGoodsSendAddr(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $addressId = I('post.addressId');
            $uid = $this->getUid(I('get.token'));
            $gid = I('post.gid');

            if(empty($gid)){
                E("empty of goods id",403);
            }

            if(empty($addressId)){
                E('empty of address id',403);
            }

            if(!is_numeric($uid)){
                E($uid,403);
            }

            $model = new Member();
            $result = $model->updateGoodsSendAddr($uid,$addressId,$gid);

            if($result){
                $ret['data'] = $result;
            }else{
                E("Error",403);
            }

        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户中奖记录里面的详细信息用户确认收货
     * 用户确认收到了商品
     */
    public function confirmGetGoods(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{

            #获取token和商品id，目的：判断用户与商品是否对应
            $uid = $this->getUid(I("get.token"));
            $gid = I('post.gid');#商品id

            if(empty($gid)){
                E('goods id is empty',403);
            }
            if(!is_numeric($uid)){
                E($uid,403);
            }
            $model = new Member();
            $result = $model->confirmGetGoods($uid,$gid);

            #判断结果
            if($result){
                $ret['data'] = $result;
            }else{
                E('Error',403);
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /* end of MemberController*/
}